package br.net.woodstock.tickettoride.jee6.web.manager;

import javax.ejb.EJB;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.faces.event.FacesEvent;
import javax.inject.Inject;
import javax.inject.Named;

import org.richfaces.event.FileUploadEvent;

import br.net.woodstock.rockframework.web.faces.security.Logon;
import br.net.woodstock.rockframework.web.faces.security.Role;
import br.net.woodstock.rockframework.web.faces.util.ExceptionHandler;
import br.net.woodstock.rockframework.web.faces.util.Log;
import br.net.woodstock.tickettoride.api.manager.ApplicationService;
import br.net.woodstock.tickettoride.api.manager.CertificateService;
import br.net.woodstock.tickettoride.web.Constants;
import br.net.woodstock.tickettoride.web.jsf.manager.common.AbstractCertificateBean;

@Named("certificateBean")
@ConversationScoped
@Log
@ExceptionHandler
@Logon
@Role({ Constants.ADMINISTRATOR_ROLE, Constants.MANAGER_ROLE })
public class CertificateBean extends AbstractCertificateBean {

	private static final long	serialVersionUID	= -334653897238184433L;

	@EJB(name = "applicationService")
	private ApplicationService	applicationService;

	@EJB(name = "certificateService")
	private CertificateService	certificateService;

	@Inject
	private Conversation		conversation;

	@Override
	public ApplicationService getApplicationService() {
		return this.applicationService;
	}

	@Override
	public CertificateService getCertificateService() {
		return this.certificateService;
	}

	@Override
	public void onStart() {
		if (!this.conversation.isTransient()) {
			this.conversation.end();
		}
		this.conversation.begin();
	}

	@Override
	public void onEnd() {
		if (!this.conversation.isTransient()) {
			this.conversation.end();
		}
	}

	@Override
	protected byte[] getBytesFromUpload(final FacesEvent event) {
		FileUploadEvent e = (FileUploadEvent) event;
		return e.getUploadedFile().getData();
	}

}
